#include <string.h>
#include "nvs_flash.h"
#include "esp_log.h"
#include "nvs_netconfig.h"
#include "utils_log.h"

#define NVS_NETWORK_FILE_NAME               "Network_cfg"

#define NVS_NETWORK_KEY_NAME_WIFI_SSID      "WiFi_ssid"
#define NVS_NETWORK_KEY_NAME_WIFI_PSW       "WiFi_psw"
#define NVS_NETWORK_KEY_NAME_WIFI_SEC_TYPE  "WiFi_sec_type"
#define NVS_NETWORK_KEY_NAME_WIFI_CONN_STATUS "conn_status"
#define NVS_NETWORK_KEY_NAME_ETH_CFG        "eth_config"
#define NVS_NETWORK_KEY_NAME_NETCONFIG_INTERFACE  "netcfg_int"
#define NVS_NETWORK_KEY_NAME_WIFI_IPCONFIG   "wifi_ipconfig"
static const char *TAG = "NVS_NETCONFIG--->";

static int GetNvsData(const char *key, void *value, const int len)
{
    if (NULL==key || NULL==value){
        LOG_TO_FILE("input params is invalid");
        return -1;
    }

    nvs_handle handle;
    esp_err_t err = nvs_open(NVS_NETWORK_FILE_NAME, NVS_READWRITE, &handle);
    if (err != ESP_OK){
        ESP_LOGE(TAG, "open %s file failed:%s",NVS_NETWORK_FILE_NAME, esp_err_to_name(err));
        LOG_TO_FILE("open %s file failed:%s",NVS_NETWORK_FILE_NAME, esp_err_to_name(err));
        return -1;
    }
    
    err = nvs_get_blob(handle, key, value, &len);
    if (err != ESP_OK){
        ESP_LOGE(TAG, "get %s blob failed:%s",key, esp_err_to_name(err));
    }

    nvs_close(handle);

    return err;
}

static int SetNvsData(const char *key, const void *value, const int len)
{
    if(NULL==key || NULL==value){
        ESP_LOGE(TAG, "input params is invalid");
        LOG_TO_FILE("input params is invalid");
        return -1;
    }

    nvs_handle handle;
    esp_err_t err = nvs_open(NVS_NETWORK_FILE_NAME, NVS_READWRITE, &handle);
    if (err != ESP_OK){
        ESP_LOGE(TAG, "open %s file failed:%s",NVS_NETWORK_FILE_NAME, esp_err_to_name(err));
        LOG_TO_FILE("open %s file failed:%s",NVS_NETWORK_FILE_NAME, esp_err_to_name(err));
        return -1;
    }
    
    err = nvs_set_blob(handle, key, value, len);
    if (err != ESP_OK){
        ESP_LOGE(TAG, "set %s blob failed:%s",key, esp_err_to_name(err));
    }

    err |= nvs_commit(handle);
    nvs_close(handle);

    return err;
}

int GetNvsWifiSsid(void *value, const int len)
{    
    return GetNvsData(NVS_NETWORK_KEY_NAME_WIFI_SSID, value, len);
}

int SetNvsWifiSsid(const void *value, const int len)
{
    return SetNvsData(NVS_NETWORK_KEY_NAME_WIFI_SSID, value, len);
}

int GetNvsWifiPsw(void *value, const int len)
{
    return GetNvsData(NVS_NETWORK_KEY_NAME_WIFI_PSW, value, len);
}

int SetNvsWifiPsw(const void *value, const int len)
{
    return SetNvsData(NVS_NETWORK_KEY_NAME_WIFI_PSW, value, len);
}

int GetNvsWifiSecType(void *value, const int len)
{
    return GetNvsData(NVS_NETWORK_KEY_NAME_WIFI_SEC_TYPE, value, len);
}

int SetNvsWifiSecType(const void *value, const int len)
{
    return SetNvsData(NVS_NETWORK_KEY_NAME_WIFI_SEC_TYPE, value, len);
}

int GetNvsWifiConnectStatus(void *value, const int len)
{
    return GetNvsData(NVS_NETWORK_KEY_NAME_WIFI_CONN_STATUS, value, len);
}

int SetNvsWifiConnectStatus(const void *value, const int len)
{
    return SetNvsData(NVS_NETWORK_KEY_NAME_WIFI_CONN_STATUS, value, len);
}

int GetNvsEthConfig(void *value, const int len)
{
    return GetNvsData(NVS_NETWORK_KEY_NAME_ETH_CFG, value, len);
}

int SetNvsEthConfig(const void *value, const int len)
{
    return SetNvsData(NVS_NETWORK_KEY_NAME_ETH_CFG, value, len);
}

int GetNvsNetconfigInterface(void *value, const int len)
{
    return GetNvsData(NVS_NETWORK_KEY_NAME_NETCONFIG_INTERFACE, value, len);
}

int SetNvsNetconfigInterface(const void *value, const int len)
{
    return SetNvsData(NVS_NETWORK_KEY_NAME_NETCONFIG_INTERFACE, value, len);
}

int GetNvsWifiIpconfig(void *value, const int len)
{
    return GetNvsData(NVS_NETWORK_KEY_NAME_WIFI_IPCONFIG, value, len);
}

int SetNvsWifiIpconfig(const void *value, const int len)
{
    return SetNvsData(NVS_NETWORK_KEY_NAME_WIFI_IPCONFIG, value, len);
}